#多用户登陆
from django.contrib.auth.backends import ModelBackend

from users.models import User

class MyLogin(ModelBackend):

    #重写ModelBackend类下面的authenticate方法
    def authenticate(self,request, username=None, password=None,**kwargs):
        try:
            #根据用户输入的username匹配数据库中username字段获取对象
            user = User.objects.filter(username = username).first()
            if not user:
                #根据用户提供的username匹配数据库mobile字段获取对象
                user = User.objects.filter(mobile = username).first()
                if not user:
                    return None
        except Exception as e:
            return None
        #判断密码是否正确
        if not user.check_password(password):
            return None

        return user

